package com.example.administrator.testproject.flagTextview;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.testproject.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class FlagActivity extends AppCompatActivity {

    private FlagTextView2 flagText;
    private ListView listView;
    private FlagAdapter adapter;
    private TextView tv;
    private EditText edit;
    private SmartRefreshLayout refresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flag);

        String icon = "icon";
        String from = "张全蛋 "+icon;
        String to = "赵铁柱";
        String txt = String.format("%s 回复@%s:我是富士康3号流水线的张全蛋，" +
                "英文名叫Micheal Jack，发文名叫helodie Jaqueline。", from, to);


        tv = (TextView)findViewById(R.id.testView);

        //设置ClickSpan,为部分文字("icon")添加点击效果
        SpannableString span = new SpannableString(txt);
//        //设置ImageSpan,占用可点击文字("icon")的位置
//        Bitmap bitmap = ImageUtils.resize(BitmapFactory.decodeResource(getResources(),
//                R.mipmap.uqmei_icon_contact), DensityUtil.sp2px(this, 12f));
//        BitmapDrawable drawable = new BitmapDrawable(bitmap);
        Drawable drawable = getResources().getDrawable(R.drawable.shape_text_flag);

        Bitmap bitmap = Bitmap.createBitmap(50, 30,Bitmap.Config.ARGB_4444);
        drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
        span.setSpan(new VerticalImageSpan(drawable),
                txt.indexOf(icon), txt.indexOf(icon) + icon.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置TextView
        tv.setText(span);
        tv.setHighlightColor(Color.TRANSPARENT);//消除点击时的背景色
        tv.setMovementMethod(LinkMovementMethod.getInstance());

        listView = (ListView)findViewById(R.id.listview);
        final List<String> data=new ArrayList<>();
        adapter = new FlagAdapter(FlagActivity.this,data);
        listView.setAdapter(adapter);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                data.add("xxxxxasdfasdfd家里附近拉丝机代理费刷卡机地方啦到数据分类");
                data.add("没一哦杀四");
                data.add("没一哦杀四");
                data.add("没一哦杀四");
                data.add("没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四水电费水电费搜索");
                data.add("没一哦二姐三杀四xxxxxasdfasdfd家里附近拉丝机代理费刷卡机地方啦");
                adapter.notifyDataSetChanged();
            }
        },1000);


        String textContent = "没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四没一哦二姐三杀四";
        flagText = (FlagTextView2)findViewById(R.id.flag_text);
//        flagText.setMaxLine(2);
        flagText.addFlagText("大学",R.drawable.shape_text_flag, getResources().getColor(R.color.gray_3),Gravity.LEFT);
        flagText.addFlagText("奥数赛",R.drawable.shape_text_flag, Color.YELLOW,Gravity.LEFT);
//        flagText.addFlagDrawable(R.mipmap.ic_auth,Gravity.RIGHT);
//        flagText.addFlagDrawable(R.mipmap.ic_auth,Gravity.RIGHT);
        flagText.setContentText(textContent);


        refresh = (SmartRefreshLayout)findViewById(R.id.refresh);
        refresh.setEnableLoadmore(true);
        refresh.setEnableRefresh(true);
    }

}
